﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace iLangl
{
	public class ErrorsRequestContext : BaseContext
	{
		IList<IError> errors = null;
		public override void SetContext(object context)
		{
			base.SetContext(context);

			if (context is IList<IError>)
			{
				errors = context as IList<IError>;
			}
		}

		public override object GetContext()
		{
			if (errors == null)
				return string.Empty;

			StringBuilder result;
			XmlWriter writer;

			CreateXmlWriter(out result, out writer);

			writer.WriteStartDocument();
			Write(writer);
			writer.WriteEndDocument();
			writer.Flush();
			return result.ToString();
		}

		public void Write(XmlWriter writer)
		{
			writer.WriteStartElement("Errors");


			InitExtendedAttributes(writer, errors);
			InitExtendedElements(writer, errors);



			foreach (IError error in errors)
			{
				writer.WriteStartElement("Error");


				writer.WriteAttributeString("message", error.Message);
				writer.WriteAttributeString("description", error.Description);
				writer.WriteAttributeString("severity", error.Severity.ToString());
				writer.WriteEndElement();
			}


			writer.WriteEndElement();//root
			
		}
	}
}
